package server.handler;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.json.JSONObject;
import server.database.RegistrationDatabase;
import server.entity.Registration;
import server.util.Base64Util;
import server.util.MD5Util;
import server.util.SessionUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

//session login
//public class LoginHandler implements HttpHandler {
//    private RegistrationDatabase db;
//    public LoginHandler() {
//        db = RegistrationDatabase.getInstance();
//    }
//
//    @Override
//    public void handle(HttpExchange exchange) throws IOException {
//
//        Headers headers = exchange.getRequestHeaders();
//
//        String contentType = headers.get("Content-Type").get(0);
//        if(contentType.equalsIgnoreCase("application/json")){
//            if (exchange.getRequestMethod().equalsIgnoreCase("POST") || exchange.getRequestMethod().equalsIgnoreCase("GET")) {
//                String inputData;
//                try{
//                    inputData = Handler.getRequestData(exchange);
//                }catch (IOException e){
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//                JSONObject jsonObject = new JSONObject(inputData);
//                //attribute validation
//                if(!jsonObject.has("username")){
//                    Handler.errorHandler(exchange, "username is required");
//                    return;
//                }
//                if(!jsonObject.has("password")){
//                    Handler.errorHandler(exchange, "password is required");
//                    return;
//                }
//                String userName = jsonObject.getString("username");
//                //Check if username exists
//                try{
//                    if(!db.hasUsername(userName)){
//                        Handler.errorHandler(exchange, "Please register as a user first");
//                        return;
//                    }
//                }catch (Exception e){
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//
//                //base64
//                String password;
//                try {
//                    password = Base64Util.decryBASE64(jsonObject.getString("password"));
//                } catch (Exception e) {
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//
//                //MD5 encryption
//                try{
//                    password = MD5Util.encryMD5(password);
//                }catch (Exception e){
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//
//                //Password validation
//                String truePassword;
//                try {
//                    truePassword = db.getRegistrationsByUsername(userName);
//                } catch (SQLException e) {
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//                if(!truePassword.equals(password)){
//                    Handler.errorHandler(exchange, "wrong password");
//                    return;
//                }
//
//                //get session
//                String session;
//                try {
//                    session = Handler.getSession(userName);
//                } catch (Exception e) {
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//
//                Handler.insertSession(session, userName);
//
//                //add session
//                exchange.getResponseHeaders().add("session", session);
//                Handler.sendResponse(exchange, "login success");
//
//            }else{
//                //Code for handling other requests
//                Handler.errorHandler(exchange,"Unsupported request method");
//            }
//        }else {
//            Handler.errorHandler(exchange, "Unsupported request contentType");
//        }
//    }
//}

//Authorization login
public class LoginHandler implements HttpHandler {

    @Override
    public void handle(HttpExchange exchange) throws IOException {

        Headers headers = exchange.getRequestHeaders();

        String contentType = headers.get("Content-Type").get(0);
        if(contentType.equalsIgnoreCase("application/json")){
            if (exchange.getRequestMethod().equalsIgnoreCase("POST") || exchange.getRequestMethod().equalsIgnoreCase("GET")) {
                Handler.sendResponse(exchange, "login success");
            }else{
                //Code for handling other requests
                Handler.errorHandler(exchange,"Unsupported request method");
            }
        }else {
            Handler.errorHandler(exchange, "Unsupported request contentType");
        }
    }
}

